/****

    activequant - activestocks.eu

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

	
	contact  : contact@activestocks.eu
    homepage : http://www.activestocks.eu

****/
package org.activequant.dao.hibernate;

import java.util.List;

import org.activequant.core.domainmodel.data.Quote;
import org.activequant.dao.IQuoteDao;
import org.activequant.util.exceptions.DaoException;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

/**
 * QuoteDao extends MarketDataEntityBase&lt;Quote&gt; implements IQuoteDao.<br/>
 * Holds the following inherited variables:
 * <ul>
 * <li>clazz(Class&lt;Quote&gt;)</li>
 * <li>hibernateTemplate(HibernateTemplate)</li>
 * </ul>
 * <br>
 * <b>History:</b><br>
 *  - [23.06.2007] Created (Erik Nijkamp)<br>
 *  - [25.08.2007] Domain model cleanup (Erik Nijkamp)<br>
 *  - [22.04.2009] Adding custom find by example (Ulrich Staudinger)<br>
 *
 *  @author Erik Nijkamp
 *  @author Ulrich Staudinger
 */
public class QuoteDao extends MarketDataEntityBase<Quote> implements IQuoteDao {
	/**
	 * Constructs a QuoteDao(extends MarketDataEntityBase&lt;Quote&gt; implements IQuoteDao) whose inherited clazz(Class&lt;T&gt;) is set to <code>Quote.class</code>.
	 */
	public QuoteDao() {
		super(Quote.class);
	}
	

	/**
	 * finds by the timestamp and instrument spec id.<br/>
	 * Looks for a persistent Quote instance whose timeStamp(TimeStamp) and instrumentSpecification(InstrumentSpecification) match the ones of the given entity(Quote).<br/>
	 * There should be one on none, otherwise its considered ambiguous and an exception is thrown.
	 */
	@SuppressWarnings("unchecked")
	public Quote findByExample(Quote entity) {
		try {			
			DetachedCriteria criteria = DetachedCriteria.forClass(Quote.class);
			//Criteria criteria = getSession().createCriteria(Quote.class);
			criteria.add(Restrictions.eq("timeStamp", entity.getTimeStamp()));
			criteria.add(Restrictions.eq("instrumentSpecification", entity.getInstrumentSpecification()));
			List list = getHibernateTemplate().findByCriteria(criteria);	
			//List list = criteria.list();
			if(list == null) {
				// no result found
				return null;
			}
			// return result
			if(list.size() == 1) {
				entity = (Quote) list.get(0);
				return entity;
			} else if(list.size() > 1) {
				throw new DaoException("Ambiguous results");
			}
			// no result found
			return null;
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}
	/**
	 * Constructs a QuoteDao(extends MarketDataEntityBase&lt;Quote&gt; implements IQuoteDao) whose inherited clazz(Class&lt;T&gt;) is set to <code>Quote.class</code>.<br/>
	 * The given sessionFactory(SessionFactory) is used to set its Hibernate SessionFactory and create/initialize the inherited hibernateTemplate(HibernateTemplate) for it.
	 * @param sessionFactory
	 */
	public QuoteDao(SessionFactory sessionFactory) {
		super(Quote.class, sessionFactory);
	}
}
